layui.use(['table', 'admin', 'ax', 'func','formSelects','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var formSelects = layui.formSelects;
    var form = layui.form;
    /**
     * 广告表管理
     */
    var Ad = {
        tableId: "adTable"
    };

    /**
     * 初始化表格的列
     */
    Ad.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'adId', align: 'center',hide: true, title: '广告id'},
            {field: 'title', align: 'center',sort: true, title: '标题'},
            {field: 'pic', align: 'center',sort: true, title: '广告图片', templet: '#pic' },
            {field: 'apId', align: 'center',hide: true, title: '位置id'},
            {field: 'positionName', align: 'center',sort: true, title: '广告位置'},
            {field: 'urlTypeName', align: 'center',sort: true, title: '链接类型', templet: function (d) {
                if(d.urlType=='0'){return  '内部链接' ; }else{ return '外部链接'; } }
                    },
            {field: 'url', hide:true,align: 'center',sort: true, title: 'URL链接'},
            {field: 'status',  align: "center",sort: true, title: '状态', templet: '#statusTpl'},
            {field: 'sort',align: 'center', sort: true, title: '排序',edit:'text'},
            {field: 'createTime',align: 'center', sort: true, title: '创建时间'},
            {field: 'updateTime',align: 'center', sort: true, title: '修改时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    //初始化所有的试样列表
    formSelects.config('selPosition', {
        searchUrl: Feng.ctxPath + "/adPosition/list",
        searchName: 'positionName',
        keyName: 'positionName',
        keyVal: 'apId'
    });

    //监听单元格编辑
    table.on('edit(adTable)', function(obj){
        var value = obj.value //得到修改后的值
            ,data = obj.data //得到所在行所有键值
            ,field = obj.field; //得到字段
        Ad.changeSort(data.adId,value);

    });

    /**
     * 修改排序
     */
    Ad.changeSort = function (id, sort) {
        var ajax = new $ax(Feng.ctxPath + "/ad/editItem", function (data) {
            Feng.success("修改成功!");
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message);
            table.reload(Ad.tableId);
        });
        ajax.set("adId", id);
        ajax.set("sort", sort);
        ajax.start();
    };

    /**
     * 点击查询按钮
     */
    Ad.search = function () {
        var queryData = {};
        var apId=formSelects.value('selPosition', 'val');
        if (apId!=""){
            apId = parseInt(apId);
            queryData['apId'] =apId;
        }else{
            queryData['apId'] ="";
        }
        queryData['title'] = $("#condition").val();
        table.reload(Ad.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Ad.openAddDlg = function () {
        func.openFull({
            title: '添加广告表',
            content: Feng.ctxPath + '/ad/add',
            tableId: Ad.tableId,
            maxmin: true,
            shade: .2,
            full:true
            //skin:'layui-layer-admin',
        });


    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    Ad.openEditDlg = function (data) {
        func.openFull({
            title: '修改广告表',
            content: Feng.ctxPath + '/ad/edit?adId=' + data.adId,
            tableId: Ad.tableId,
            maxmin: true,
        });
    };

    /**
     * 导出excel按钮
     */
    Ad.exportExcel = function () {
        var checkRows = table.checkStatus(Ad.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Ad.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/ad/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Ad.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("adId", data.adId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    /*
    *点击图片
     */
    Ad.openImage=function (data) {
        layer.open({
            type: 1,
            title: false,
            closeBtn: 1,
            area: ['auto'],
            skin: 'layui-layer-nobg', //没有背景色
            shadeClose: true,
            content: "<div class='hide' ><img src='"+data+"' style='max-height: 600px'></div> "
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Ad.tableId,
        url: Feng.ctxPath + '/ad/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Ad.initColumn()


    });


    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Ad.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Ad.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Ad.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Ad.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Ad.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Ad.onDeleteItem(data);
        }
        else if (layEvent === 'open_image') {
            Ad.openImage(data.pic);

        }
    });



    // 修改user状态
    form.on('switch(status)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;
        Ad.changeStatus(id, checked);
    });

    /**
     * 修改启用状态
     */
    Ad.changeStatus = function (id, checked) {
        var ajax = new $ax(Feng.ctxPath + "/ad/changeStatus", function (data) {
            Feng.success("修改成功!");
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message);
            table.reload(AdPosition.tableId);
        });
        ajax.set("id", id);
        ajax.set("status", checked);
        ajax.start();
    };
});
